package com.doge.walknovel.business.novel

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.doge.walknovel.api.model.NovelDetailInfo
import com.doge.walknovel.api.model.CommentInfo
import com.doge.walknovel.business.novel.viewholders.NovelCommentsTopViewHolder
import com.doge.walknovel.business.novel.viewholders.NovelCommentsViewHolder
import com.doge.walknovel.business.novel.viewholders.NovelInfoViewHolder
import com.doge.walknovel.business.novel.viewholders.NovelMightLikeViewHolder
import com.doge.walknovel.core.base.adapters.BasePagedAdapter
import com.doge.walknovel.utils.dp
import com.doge.walknovel.utils.log

const val CHAPTERS = "chapters"
const val RATING = "rating"
const val MIGHT_LIKE = "mightLike"
const val COMMENTS_INPUT = "commentsInput"
const val COMMENTS_REPLY = "commentsReply"
const val COMMENTS_COLLECT = "commentsCollect"
const val COMMENTS_MORE = "commentsMore"

class NovelDetailAdapter : BasePagedAdapter() {
    private var topInfo: NovelInfoViewHolder? = null

    fun slideInTop(): Boolean {
        log("NovelDetailAdapter slideInTop topInfoView top ${topInfo?.itemView?.top}")
        val top = topInfo ?: return false
        return top.itemView.top > (-100).dp
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            ItemType.Info -> NovelInfoViewHolder(parent, callback)
            ItemType.MightLike -> NovelMightLikeViewHolder(parent, callback)
            ItemType.CommentTop -> NovelCommentsTopViewHolder(parent, callback)
            ItemType.Comment -> NovelCommentsViewHolder(parent, callback)
            else -> super.onCreateViewHolder(parent, viewType)
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val item = getItem(position)
        when (holder) {
            is NovelInfoViewHolder -> item?.data<NovelDetailInfo>()?.let { holder.bind(it) }
            is NovelMightLikeViewHolder -> item?.data<NovelDetailInfo>()?.let { holder.bind(it) }
            is NovelCommentsTopViewHolder -> item?.data<NovelDetailInfo>()?.let { holder.bind(it) }
            is NovelCommentsViewHolder -> item?.data<CommentInfo>()?.let { holder.bind(it) }
        }
    }

    override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
        super.onViewAttachedToWindow(holder)
        if (holder is NovelInfoViewHolder) topInfo = holder
    }

    override fun onViewDetachedFromWindow(holder: RecyclerView.ViewHolder) {
        super.onViewDetachedFromWindow(holder)
        if (holder is NovelInfoViewHolder) topInfo = null
    }
}
